package order.controller;

import lombok.extern.slf4j.Slf4j;
import order.dao.OrderInfo;
import order.server.OrderServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderServer orderServer;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;

    @RequestMapping("/{orderId}")
    public OrderInfo getOrder(@PathVariable("orderId") int orderId) {
        List<ServiceInstance> list = discoveryClient.getInstances("product-server");
        List<ServiceInstance> list1 = discoveryClient.getInstances("order-server");
        OrderInfo o = orderServer.getOrderById(orderId);
        log.info(o.toString());
        return o;
    }

    @RequestMapping("/self/self")
    public String self() {
        System.out.println("hhh");
        String url = "http://order-server/order/self";
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }

    @RequestMapping("/self")
    public String self1() {
        return "hhh";
    }

    @RequestMapping("/r1/r1")
    public String r1() {
        System.out.println("hhhh");
        String url = "http://hadage/self";
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }


}
